package com.apes.scm.masterdata.area.service;

import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.cache.Cache;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.area.model.Area;
import com.apes.scm.masterdata.area.repository.AreaRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 功能：区域信息服务
 *
 * @author xul
 * @create 2018-06-11 11:16
 */
@Service("areaService")
public class AreaService extends DomainService {
    private final String CONST_DEFAULT_CACHE_NAME = "areaCache";
    private final String CONST_DEFAULT_CACHE_NAME_INIT_FLAG = "areaCacheInitFlag";

    @Autowired
    AreaRepository areaRepository;

    @Autowired
    private Cache cache;

    public List findAll(SimpleRequest request) {
        String k = "areas";
        Boolean init = (Boolean) cache.get(CONST_DEFAULT_CACHE_NAME, CONST_DEFAULT_CACHE_NAME_INIT_FLAG);
        if (init == null || !init) {
            List<Area> areas = areaRepository.findAll();
            cache.put(CONST_DEFAULT_CACHE_NAME, k, MapUtil.mapped(areas));
            cache.put(CONST_DEFAULT_CACHE_NAME, CONST_DEFAULT_CACHE_NAME_INIT_FLAG, true);
        }
        return (List) cache.get(CONST_DEFAULT_CACHE_NAME, k);
    }

    public Page<Area> find(SimpleRequest request) {
        return areaRepository.findAll(PageRequest.of(request.get("page"), request.get("size")));
    }

    public Area findOne(SimpleRequest request) {
        return request.getJO().containsKey("id") ? areaRepository.findOne(request.get("id").toString()) : null;
    }

    public List<Area> findProvince(SimpleRequest request) {
        return areaRepository.findByParentIsNull();
    }

    public Page findProvinceByPage(SimpleRequest request) {
        JSONObject jo = request.getJO();
        String likeKey = jo.containsKey("key") ? jo.getString("key") : "";
        Page<Map> result = areaRepository.findByProvince(likeKey,PageRequest.of(jo.getInteger("page"), jo.getInteger("size")));
        return result;
    }

    public Page findCityByPage(SimpleRequest request) {
        JSONObject jo = request.getJO();
        String likeKey = jo.containsKey("key") ? jo.getString("key") : "";
        Page<Map> result = areaRepository.findByCity(likeKey,PageRequest.of(jo.getInteger("page"), jo.getInteger("size")));
        return result;
    }

    public List<Area> findChildren(SimpleRequest request) {
        return areaRepository.findByParent(request.get("id").toString());
    }

    /**
     * 根据默认城市代码，查找城市
     *
     * @param code
     * @return
     */
    public Area findByStyleCode(String code) {
        Area area;
        int codeLength = code.length();
        switch (codeLength) {
            case 2:
                area = areaRepository.findOne(code + "0000");
                break;
            case 4:
                area = areaRepository.findOne(code + "00");
                break;
            case 6:
                area = areaRepository.findOne(code);
                break;
            default:
                throw new RuntimeException("城市编码不存在");
        }
        return area;
    }
}
